package com.faithlibraryplus;

import android.annotation.SuppressLint;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import resources.Item;
import resources.Macro;
import resources.Member;
/**
 *
 * @author fangchengjia
 */
public class Helper {    
    public static String encodeValues(String values[]){
        String formated = "";
        for (String s: values){
            formated += String.format("'%s',", Codecs.encode(s));
        }
        return formated.substring(0, formated.length()-1);
    }
    
    public static String encodeValues(String table[], String values[]){
        String formated = "";
        int index = 0;
        for (String s: values){
            formated += String.format(
                    "%s='%s',", 
                    table[index++], 
                    Codecs.encode(s));
        }
        return formated.substring(0, formated.length()-1);
    }
    
    public static Item findItemById(List<Item> items, String id){
    	Item result = null;
    	for(Item item: items){
    		if (item.getId().equals(id)){
    			result = item;
    			break;
    		}
    	}
    	return result;
    }
    
    public static List<Item> findItemsByCallNumber(List<Item> items, String call_num){
    	List<Item> result = new ArrayList<Item>();
    	for(Item item: items){
    		if (item.getCallNumber().contains(call_num)){
    			result.add(item);
    		}
    	}
    	return result;
    }
    
    public static List<Item> findItemsByISBN(List<Item> items, String isbn){
    	List<Item> result = new ArrayList<Item>();
    	for(Item item: items){
    		if (item.getISBN().contains(isbn)){
    			result.add(item);
    		}
    	}
    	return result;
    }
    
    public static List<Item> findItemsByStatus(List<Item> items, String status){
		List<Item> result = new ArrayList<Item>();
		for (Item item: items){
			if (item.getStatus().equals(status)){
				result.add(item);
			}
		}
		return result;
	}
    
    public static int findMemberIndexByName(List<Member> members, String name){
    	int index = 0;
    	boolean found = false;
    	for(Member member: members){
    		if (member.getName().equals(name)){
    			index++;
    			found = true;
    			break;
    		}
    		index++;
    	}
    	return found ? index : 0;
    }
    
    @SuppressLint("SimpleDateFormat")
	public static String getFutureDateFromToday(int days){
        Calendar calender = Calendar.getInstance();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        calender.setTime(new Date());
        calender.add(Calendar.DATE, days);
        return dateFormat.format(calender.getTime());
    }
    
    public static Item getItemById(List<Item> items, String id){
    	Item result = null;
    	for (Item item: items){
    		if (item.getId().equals(id)){
    			result = item;
    			break;
    		}
    	}
    	return result;
    }
    
    public static List<Item> itemBuilder(List<String> lines){
		List<Item> items = new ArrayList<Item>();
		for (String line: lines){
			String[] data = line.split(",");
			Item item = new Item(Codecs.decode(data[0]), Codecs.decode(data[1]),
					Codecs.decode(data[2]), Codecs.decode(data[3]), Codecs.decode(data[4]),
					Codecs.decode(data[5]), Codecs.decode(data[6]), Codecs.decode(data[7]),
					Codecs.decode(data[8]), Codecs.decode(data[9]), Codecs.decode(data[10]),
					Codecs.decode(data[11]), Codecs.decode(data[12]), Codecs.decode(data[13]),
					Codecs.decode(data[14]));
        	items.add(item);
		}
		return items;
	}
    
    public static List<Member> memberBuilder(List<String> lines){
		List<Member> members = new ArrayList<Member>();
		for (String line: lines){
			String[] data = line.split(",");
			Member item = new Member(Codecs.decode(data[0]), Codecs.decode(data[1]),
					Codecs.decode(data[2]), Codecs.decode(data[3]), Codecs.decode(data[4]),
					Codecs.decode(data[5]), Codecs.decode(data[6]), Codecs.decode(data[7]),
					Codecs.decode(data[8]), Codecs.decode(data[9]), Codecs.decode(data[10]),
					Codecs.decode(data[11]), Codecs.decode(data[12]), Codecs.decode(data[13]));
			members.add(item);
		}
		return members;
	}
    
    public static Member getMeberById(List<Member> members, String id){
    	Member result = null;
    	for (Member member: members){
    		if (member.getId().equals(id)){
    			result = member;
    			break;
    		}
    	}
    	return result;
    }
    
    public static List<String> reformatItems(List<Item> items){
    	List<String> list = new ArrayList<String>();
    	for (Item item: items){
    		list.add(String.format("%s,%s", item.getName(), item.getId()));
    	}
    	return list;
    }
    
    public static List<String> reformatMembers(List<Member> members){
    	List<String> list = new ArrayList<String>();
    	list.add(Macro.ITEM_OWNER_NONE);
    	for (Member member: members){
    		list.add(String.format("%s,%s", member.getName(), member.getId()));
    	}
    	return list;
    }
}
